package com.ruoyi.drone.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.drone.domain.DroneTaskCategory;
import com.ruoyi.drone.domain.DroneTask;
import com.ruoyi.drone.service.IDroneTaskCategoryService;
import com.ruoyi.drone.service.IDroneTaskService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 任务分类Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/drone/taskCategory")
public class DroneTaskCategoryController extends BaseController
{
    @Autowired
    private IDroneTaskCategoryService droneTaskCategoryService;
    
    @Autowired
    private IDroneTaskService droneTaskService;

    /**
     * 查询任务分类列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DroneTaskCategory droneTaskCategory)
    {
        startPage();
        List<DroneTaskCategory> list = droneTaskCategoryService.selectDroneTaskCategoryList(droneTaskCategory);
        return getDataTable(list);
    }

    /**
     * 查询所有启用的任务分类
     */
    @GetMapping("/listEnabled")
    public AjaxResult listEnabled()
    {
        List<DroneTaskCategory> list = droneTaskCategoryService.selectAllEnabledCategories();
        return success(list);
    }

    /**
     * 根据分类ID查询已发布的任务列表
     * 组织和飞手都可以查看
     */
    @GetMapping("/tasks/{categoryId}")
    public TableDataInfo getTasksByCategory(@PathVariable("categoryId") Long categoryId)
    {
        DroneTask task = new DroneTask();
        task.setCategoryId(categoryId);
        task.setStatus("1"); // 只查询已发布的任务
        startPage();
        List<DroneTask> list = droneTaskService.selectDroneTaskList(task);
        return getDataTable(list);
    }

    /**
     * 查询所有已发布的任务列表（不限制分类）
     * 组织和飞手都可以查看
     */
    @GetMapping("/tasks")
    public TableDataInfo getAllPublishedTasks()
    {
        DroneTask task = new DroneTask();
        task.setStatus("1"); // 只查询已发布的任务
        startPage();
        List<DroneTask> list = droneTaskService.selectDroneTaskList(task);
        return getDataTable(list);
    }


    /**
     * 获取任务分类详细信息
     */
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(droneTaskCategoryService.selectDroneTaskCategoryByCategoryId(categoryId));
    }

    /**
     * 新增任务分类
     */
    @Log(title = "任务分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DroneTaskCategory droneTaskCategory)
    {
        if (!droneTaskCategoryService.checkCategoryCodeUnique(droneTaskCategory.getCategoryCode()))
        {
            return error("新增任务分类'" + droneTaskCategory.getCategoryName() + "'失败，分类编码已存在");
        }
        return toAjax(droneTaskCategoryService.insertDroneTaskCategory(droneTaskCategory));
    }

    /**
     * 修改任务分类
     */
    @Log(title = "任务分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DroneTaskCategory droneTaskCategory)
    {
        if (!droneTaskCategoryService.checkCategoryCodeUnique(droneTaskCategory.getCategoryCode()))
        {
            return error("修改任务分类'" + droneTaskCategory.getCategoryName() + "'失败，分类编码已存在");
        }
        return toAjax(droneTaskCategoryService.updateDroneTaskCategory(droneTaskCategory));
    }

    /**
     * 删除任务分类
     */
    @Log(title = "任务分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        return toAjax(droneTaskCategoryService.deleteDroneTaskCategoryByCategoryIds(categoryIds));
    }

    /**
     * 导出任务分类列表
     */
    @Log(title = "任务分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DroneTaskCategory droneTaskCategory)
    {
        List<DroneTaskCategory> list = droneTaskCategoryService.selectDroneTaskCategoryList(droneTaskCategory);
        ExcelUtil<DroneTaskCategory> util = new ExcelUtil<DroneTaskCategory>(DroneTaskCategory.class);
        util.exportExcel(list, "任务分类数据");
    }
}
